/**
 * 
 */
package info.niwota.frames;

import info.niwota.commons.io.Files;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

/**
 * @author qiangli
 * 
 */
public class CaptureActivity extends Activity {
	public static final String EXT_JPG = ".jpg";

	private static final String IMAGE_PREFIX = "photo-";

	private static final int RC_TAKE_PICTURE = 12;

	private static final String SAVED_TEMPFILE = "tempfile";

	private static final String TAG = "CaptureActivity";

	private File tempFile;

	private void doReturn(File file) {
		try {
			Uri uri = Uri.fromFile(file);
			Intent i = new Intent(null, uri);
			//
			if (Debug.DEBUG) {
				ComponentName ca = getCallingActivity();
				Log.d(TAG, "returning: " + ca + " uri: " + uri);
			}
			setResult(RESULT_OK, i);
		} catch (Exception e) {
			setResult(RESULT_CANCELED);
			// TODO
			e.printStackTrace();
		}
	}

	private String makePrefix(String prefix) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
		return prefix + df.format(new Date());
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onActivityResult resultCode " + resultCode + " data: "
					+ data + " tempFile: " + tempFile);
		}
		setResult(resultCode);//
		if (resultCode == RESULT_CANCELED) {
			if (tempFile != null) {
				tempFile.delete();
			}
		} else if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case RC_TAKE_PICTURE: {
				doReturn(tempFile);
				break;
			}
			default:
				//
			}
		}
		try {
			finishActivity(requestCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finish();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (Debug.DEBUG) {
			Log.d(TAG, "onCreate bundle: " + savedInstanceState + " file: "
					+ tempFile);
		}
		if (savedInstanceState == null) {
			String act = getIntent().getAction();

			if (Debug.DEBUG) {
				Log.d(TAG, "onCreate new request: " + act);
			}
			//
			startCameraActivity();
			//
		} else {
			tempFile = (File) savedInstanceState
					.getSerializable(SAVED_TEMPFILE);

			if (Debug.DEBUG) {
				Log.d(TAG, "onCreate restored: " + tempFile);
			}
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onSaveInstanceState file: " + tempFile + " outState: "
					+ outState);
		}
		outState.putSerializable(SAVED_TEMPFILE, tempFile);
		//
		super.onSaveInstanceState(outState);
	}

	private void startCameraActivity() {
		try {
			Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			String prefix = makePrefix(IMAGE_PREFIX);
			File sub = Files.getSubDir(this, "images");
			if (!sub.exists()) {
				sub.mkdirs();
			}
			tempFile = File.createTempFile(prefix, EXT_JPG, sub);
			 
			Uri uri = Uri.fromFile(tempFile);
			//
			if (Debug.DEBUG) {
				Log.d(TAG, "startCameraActivity uri: " + uri);
			}
			//
			i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
			//
			startActivityForResult(i, RC_TAKE_PICTURE);
		} catch (Exception e) {
			e.printStackTrace();
			toast("" + e.getMessage());
			setResult(RESULT_CANCELED);
			finish();
		}
	}

	private void toast(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}
}
